const regeneratorRuntime = require('../utils/regenerator-runtime.js');
let app_str = "https://qa.tianfuhoney.com" //测试
//let app_str = "https://prod.tianfuhoney.com"  //正式


const app = getApp();

var header = {
  'content-type': 'application/json',
  'X-Access-Token': '',
};

function POST(url, params) {
  const value = wx.getStorageSync('token');
  wx.showLoading({
    title: '加载中',
    mask: true
  })
  try {
    header['X-Access-Token'] = value
  } catch (e) {
    header['X-Access-Token'] = null
  }
  let promise = new Promise(function(resolve, reject) {
    wx.request({
      url: app_str + url,
      data: params,
      method: 'POST',
      header: header,
      success: function(res) {
        if (res.data.status == 500) {
          wx.hideLoading()
          wx.reLaunch({
            url: '../login/login',
            success: function () {
              return false
            }
          })
        }
        if (res.data.code == 200) {
          resolve(res);
          wx.hideLoading()
        } else {
          if (res.data.code == 510) {
            wx.reLaunch({
              url: '../login/login',
            })
            wx.hideLoading()
          } else {
            wx.showToast({
              title: res.data.message ? res.data.message : res.data.msg,
              icon: 'none',
              duration: 3000,
              mask: true,
              success: function() {
                setTimeout(function() {
                  wx.hideLoading()
                }, 1500)
              }
            })
          }
        }
      },
      fail: function(err) {
        wx.hideLoading()
        console.log(err)
      }
    })
  });
  return promise
}

function GET(url, params) {
  const value = wx.getStorageSync('token');
  wx.showLoading({
    title: '加载中',
    mask: true
  })
  try {
    header['X-Access-Token'] = value
  } catch (e) {
    header['X-Access-Token'] = null
  }
  let promise = new Promise(function(resolve, reject) {
    wx.request({
      url: app_str + url,
      data: params,
      header: header,
      method: 'GET',
      success: function(res) {
        console.log("成功" + res.data.status)
        if (res.data.status == 500) {
          wx.hideLoading()
          wx.reLaunch({
            url: '../login/login',
            success:function(){
              return false
            }
          })
        }else{
          if (res.data.code == 200) {
            wx.hideLoading()
            resolve(res);
          } else {
            if (res.data.code == 10010) {
              wx.navigateTo({
                url: '../beebox/add/addbeebox',
              })
              wx.hideLoading()
            } else if (res.data.code == 10011) {
              wx.navigateTo({
                url: '../new_user/newUser',
              })
              wx.hideLoading()
            } else {
              wx.showToast({
                title: res.data.message ? res.data.message : res.data.msg,
                icon: 'none',
                duration: 3000,
                mask: true,
                success: function () {
                  setTimeout(function () {
                    wx.hideLoading()
                  }, 1500)
                }
              })
            }
          }
        }

      },
      fail: function(err) {
        wx.hideLoading()
        console.log(err)
      }
    })
  });
  return promise
}

function PUT(url, params) {
  const value = wx.getStorageSync('token');
  wx.showLoading({
    title: '加载中',
    mask: true
  })
  try {
    header['X-Access-Token'] = value
  } catch (e) {
    header['X-Access-Token'] = null
  }
  let promise = new Promise(function(resolve, reject) {
    wx.request({
      url: app_str + url,
      data: params,
      method: 'PUT',
      header: header,
      success: function(res) {
        if (res.data.status == 500) {
          wx.reLaunch({
            url: '../login/login',
            success: function () {
              return false
            }
          })
        }
        if (res.data.code !== 200) {
          wx.showToast({
            title: res.data.message ? res.data.message : res.data.msg,
            icon: 'none',
            duration: 3000,
            mask: true,
            success: function() {
              setTimeout(function() {
                wx.hideLoading()
              }, 1500)

            }
          })
        } else {
          resolve(res);
          wx.hideLoading()
        }
      },
      fail: function(err) {
        wx.hideLoading()
        console.log(err)
      }
    })
  });
  return promise
}
module.exports = {
  postdata: POST,
  getdata: GET,
  putdata: PUT,
  headervar: header,
  test_str: app_str
};
